package com.example.imageswitcher

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*
/**
 * Created by 舍长
 * describe:实现图片切换淡入，淡出效果
 */
class MainActivity : AppCompatActivity() {
    var i = 0//控制点击切换图片
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        imageSwitcher.setFactory {
            val imageView= ImageView(this)
            imageView
        }
        //默认显示图片
        setImage1()
        //点击一切换图片，实现淡入，淡出效果
        btn_01.setOnClickListener {
            if (i == 0) {
                setImage1()
                i = 1
            } else {
                i = 0
                setImage2()
            }
        }
    }

    /**
     * 设置图片2
     */
    private fun setImage2() {
        imageSwitcher.setInAnimation(this, android.R.anim.fade_in)
        imageSwitcher.setOutAnimation(this, android.R.anim.fade_out)
        imageSwitcher.setImageResource(R.drawable.iv_recycler_g)
    }

    /**
     * 设置图片1
     */
    private fun setImage1() {
        imageSwitcher.setInAnimation(this, android.R.anim.fade_in)
        imageSwitcher.setOutAnimation(this, android.R.anim.fade_out)
        imageSwitcher.setImageResource(R.drawable.iv_recycler_f)
    }
}
